﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Trade.Controls;
using Trade.DAL;
using Trade.Model;

namespace Trade
{
    public partial class FrmAccountsPayable : Trade.Controls.RayForm
    {
        private AccountsPayable accountsPayable = new AccountsPayable();
        private Utility.CRUD dataState = Utility.CRUD.None;
        private Common common = new Common();
        private DataTable dtAccountsPayable = null;

        public FrmAccountsPayable()
        {
            InitializeComponent();
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);

            btnOperatorGroup.btnNew.Click += new EventHandler(btnNew_Click);
            btnOperatorGroup.btnModify.Click += new EventHandler(btnModify_Click);
            btnOperatorGroup.btnDelete.Click += new EventHandler(btnDelete_Click);
            btnOperatorGroup.btnSave.Click += new EventHandler(btnSave_Click);
            btnOperatorGroup.btnCancel.Click += new EventHandler(btnCancel_Click);
            btnOperatorGroup.btnQuit.Click += new EventHandler(btnQuit_Click);
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            if (dataState != Utility.CRUD.None)
            {
                if (MessageBox.Show("当前数据还未保存,是否需要保存数据?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                {
                    btnOperatorGroup.btnSave.Focus();
                    return;
                }
            }

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.None;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);
            dgvAccountsPayable.Enabled = true;
            dgvAccountsPayable.Focus();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int recordId = 0;

            if (String.IsNullOrEmpty(txtReferenceNo.Text.Trim()))
            {
                MessageBox.Show("货单编号不能为空！", "Prompt ...");
                txtReferenceNo.Focus();
                return;
            }

            if (String.IsNullOrEmpty(cmbEmployee.Text.Trim()))
            {
                MessageBox.Show("经手人不能为空！", "Prompt ...");
                cmbEmployee.Focus();
                return;
            }

            if (String.IsNullOrEmpty(txtAmount.Text.Trim()))
            {
                MessageBox.Show("应付应收金额不能为空！", "Prompt ...");
                txtAmount.Focus();
                return;
            }
            else
            {
                if (Convert.ToDecimal(txtAmount.Text.Trim()) == 0)
                {
                    MessageBox.Show("应付应收金额不能为零！", "Prompt ...");
                    txtAmount.Focus();
                    return;
                }
            }

            AccountsPayableInfo accountsPayableInfo = new AccountsPayableInfo();
            accountsPayableInfo.BillNo = txtBillNo.Text.Trim();
            accountsPayableInfo.Category = Convert.ToInt32(cmbCategory.SelectedValue.ToString());
            accountsPayableInfo.PayDate = dtpPayDate.Value;
            accountsPayableInfo.Employee = cmbEmployee.SelectedValue.ToString().Trim();
            accountsPayableInfo.ReferenceNo = txtReferenceNo.Text.Trim();
            accountsPayableInfo.Amount = String.IsNullOrEmpty(txtAmount.Text.Trim()) ? 0: Convert.ToInt32(txtAmount.Text.Trim());
            accountsPayableInfo.Account = txtAccount.Text.Trim();
            accountsPayableInfo.EntryDate = dtpEntryDate.Value;
            accountsPayableInfo.Remark = txtRemark.Text.Trim();
            accountsPayableInfo.RecordId = dataState == Utility.CRUD.Insert ? 0 : (int)dgvAccountsPayable.CurrentRow.Cells["RecordId"].Value;

            accountsPayable.WriteToDatabase(Utility.CRUDAction(dataState), accountsPayableInfo, out recordId);

            if (dataState == Utility.CRUD.Insert)
            {
                DataRow dr = accountsPayable.GetAccountsPayableByRecordId(recordId);
                dtAccountsPayable.ImportRow(dr);
                dgvAccountsPayable.CurrentRow.Selected = false;
                dgvAccountsPayable.Rows[dgvAccountsPayable.RowCount - 1].Selected = true;
                dgvAccountsPayable.CurrentCell = dgvAccountsPayable.Rows[dgvAccountsPayable.RowCount - 1].Cells["BillNo"];     //让当前选中行的Cell得到焦点
                //dgvAccountsPayable_SelectionChanged(sender, e);
            }
            else if (dataState == Utility.CRUD.Update)
            {
                dgvAccountsPayable.CurrentRow.Cells["BillNo"].Value = accountsPayableInfo.BillNo;
                dgvAccountsPayable.CurrentRow.Cells["Category"].Value = accountsPayableInfo.Category;
                dgvAccountsPayable.CurrentRow.Cells["PayDate"].Value = accountsPayableInfo.PayDate;
                dgvAccountsPayable.CurrentRow.Cells["Employee"].Value = accountsPayableInfo.Employee;
                dgvAccountsPayable.CurrentRow.Cells["ReferenceNo"].Value = accountsPayableInfo.ReferenceNo;
                dgvAccountsPayable.CurrentRow.Cells["Amount"].Value = accountsPayableInfo.Amount;
                dgvAccountsPayable.CurrentRow.Cells["Account"].Value = accountsPayableInfo.Account;
                dgvAccountsPayable.CurrentRow.Cells["EntryDate"].Value = accountsPayableInfo.EntryDate;
                dgvAccountsPayable.CurrentRow.Cells["Remark"].Value = accountsPayableInfo.Remark;
                dgvAccountsPayable.CurrentRow.Cells["RecordId"].Value = accountsPayableInfo.RecordId;
            }
            else if (dataState == Utility.CRUD.Delete)
            {
                dgvAccountsPayable.Rows.Remove(dgvAccountsPayable.Rows[dgvAccountsPayable.CurrentRow.Index]);
            }

            dataState = Utility.CRUD.None;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);
            dgvAccountsPayable.Enabled = true;
            dgvAccountsPayable.Focus();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvAccountsPayable.RowCount != 0)
            {
                if (MessageBox.Show("是否真的删除当前数据?", "删除数据", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                {
                    dataState = Utility.CRUD.Delete;
                    common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
                    dgvAccountsPayable.Enabled = false;
                }
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.Update;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            AccountsPayableInfo accountsPayableInfo = new AccountsPayableInfo();
            accountsPayableInfo.RecordId = (int)dgvAccountsPayable.CurrentRow.Cells["RecordId"].Value;

            common.SetControlsState(this, true);
            dgvAccountsPayable.Enabled = false;
            txtBillNo.Enabled = false;
            txtReferenceNo.Enabled = false;
            cmbCategory.Focus();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.Insert;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, true);
            dgvAccountsPayable.Enabled = false;

            txtBillNo.Enabled = false;
            txtBillNo.Text = accountsPayable.GenAccountPayableBillNo(DateTime.Now.ToString("yyyyMMdd")).Trim();
            cmbCategory.Focus();
        }

        private void SetAccountsPayableDataGridView()
        {
            dgvAccountsPayable.AllowUserToAddRows = false;
            dgvAccountsPayable.AllowUserToDeleteRows = false;
            dgvAccountsPayable.ReadOnly = true;

            dgvAccountsPayable.Columns[0].Width = 100;
            dgvAccountsPayable.Columns[0].Name = dtAccountsPayable.Columns["BillNo"].ColumnName.ToString();
            dgvAccountsPayable.Columns[0].HeaderText = "账单编号";
            dgvAccountsPayable.Columns[1].Width = 200;
            dgvAccountsPayable.Columns[1].Name = dtAccountsPayable.Columns["Category"].ColumnName.ToString();
            dgvAccountsPayable.Columns[1].HeaderText = "款项类型";
            dgvAccountsPayable.Columns[2].Width = 100;
            dgvAccountsPayable.Columns[2].Name = dtAccountsPayable.Columns["CategoryName"].ColumnName.ToString();
            dgvAccountsPayable.Columns[2].HeaderText = "类型名称";
            dgvAccountsPayable.Columns[3].Width = 100;
            dgvAccountsPayable.Columns[3].Name = dtAccountsPayable.Columns["PayDate"].ColumnName.ToString();
            dgvAccountsPayable.Columns[3].HeaderText = "付款日期";
            dgvAccountsPayable.Columns[4].Width = 100;
            dgvAccountsPayable.Columns[4].Name = dtAccountsPayable.Columns["Employee"].ColumnName.ToString();
            dgvAccountsPayable.Columns[4].HeaderText = "经手人";
            dgvAccountsPayable.Columns[4].Frozen = true;
            dgvAccountsPayable.Columns[5].Width = 80;
            dgvAccountsPayable.Columns[5].Name = dtAccountsPayable.Columns["ReferenceNo"].ColumnName.ToString();
            dgvAccountsPayable.Columns[5].HeaderText = "相应货单编号";
            dgvAccountsPayable.Columns[5].Visible = false;
            dgvAccountsPayable.Columns[6].Width = 100;
            dgvAccountsPayable.Columns[6].Name = dtAccountsPayable.Columns["Amount"].ColumnName.ToString();
            dgvAccountsPayable.Columns[6].HeaderText = "金额";
            dgvAccountsPayable.Columns[7].Width = 80;
            dgvAccountsPayable.Columns[7].Name = dtAccountsPayable.Columns["Account"].ColumnName.ToString();
            dgvAccountsPayable.Columns[7].HeaderText = "账户";
            dgvAccountsPayable.Columns[8].Width = 80;
            dgvAccountsPayable.Columns[8].Name = dtAccountsPayable.Columns["EntryDate"].ColumnName.ToString();
            dgvAccountsPayable.Columns[8].HeaderText = "录入日期";
            dgvAccountsPayable.Columns[9].Width = 150;
            dgvAccountsPayable.Columns[9].Name = dtAccountsPayable.Columns["Remark"].ColumnName.ToString();
            dgvAccountsPayable.Columns[9].HeaderText = "备注";
            dgvAccountsPayable.Columns[10].Width = 80;
            dgvAccountsPayable.Columns[10].Name = dtAccountsPayable.Columns["RecordId"].ColumnName.ToString();
            dgvAccountsPayable.Columns[10].HeaderText = "记录ID";
            dgvAccountsPayable.Columns[10].Visible = false;

            dgvAccountsPayable.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }

        private void FrmAccountsPayable_Load(object sender, EventArgs e)
        {
            //dtAccountsPayable = accountsPayable.AccountsPayableTemplate();

            //bs = new BindingSource(this.dtAccountsPayable, null);
            //dgvAccountsPayable.DataSource = bs;
            //SetAccountsPayableDataGridView();

            dtAccountsPayable = accountsPayable.GetAccountsPayableList();

            dgvAccountsPayable.DataSource = dtAccountsPayable;
            dgvAccountsPayable.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            SetAccountsPayableDataGridView();

            cmbCategory.Tag = "P000";
            common.ComboBoxBindingDataSource(cmbCategory, "SubjectName", "SubjectName");

            common.EmployeeComboBoxDataBinding(cmbEmployee, "ChineseName", "ChineseName");

            if (dgvAccountsPayable.CurrentRow != null)
            {
                dgvAccountsPayable.CurrentRow.Selected = true;
            }

            dgvAccountsPayable.Focus();
        }
    }
}
